package com.igeek.yygh.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.yygh.listener.MyAnalysisEventListener;
import com.igeek.yygh.mapper.DictMapper;
import com.igeek.yygh.model.cmn.Dict;
import com.igeek.yygh.service.DictService;
import com.igeek.yygh.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zengxiangjun
 * @Date 2022/8/5  13:55 DictServiceImpl
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    //通过数据字典的id主键，查询数据字典列表 ，并且还要携带是否有子级数据
    @Override
    // @Cacheable(value = "dict" , keyGenerator = "keyGenerator")
    public List<Dict> findAllById(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", id);
        //根据parentId查询数据(当前List中仅有表中的数据，没有hasChildren)
        List<Dict> dictList = baseMapper.selectList(queryWrapper);
        //设置hasChildren属性
        for (Dict dict : dictList) {
            dict.setHasChildren(isChildren(dict.getId()));
        }
        return dictList;
    }

    //判断是否有子级数据
    private boolean isChildren(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", id);
        Long count = baseMapper.selectCount(queryWrapper);
        //此时count>0 说明有子级数据，则返回true
        return count > 0;
    }

    //导出
    @Override
    public void exportDictData(HttpServletResponse response) throws IOException {
        //1.设置响应信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("数据字典", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        //2.准备数据
        List<Dict> dicts = baseMapper.selectList(null);
        List<DictEeVo> dictEeVoList = new ArrayList<>();
        for (Dict dict : dicts) {
            DictEeVo dictEeVo = new DictEeVo();
            BeanUtils.copyProperties(dict, dictEeVo);
            dictEeVoList.add(dictEeVo);
        }

        //3.写
        EasyExcel.write(response.getOutputStream(), DictEeVo.class).sheet("数据字典").doWrite(dictEeVoList);
    }


    //导入
    @Override
    @CacheEvict(value = "dict", allEntries = true)   //allEntries = true 方法调用后将立即清空所有的缓存
    public void importDictData(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), DictEeVo.class, new MyAnalysisEventListener(baseMapper)).sheet().doRead();
    }
}
